package parspack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;

public class MyCompFighter implements MyCompilationUnit {
	ICompilationUnit unit;
	MyCompFighter(ICompilationUnit u){
		this.unit = u;
	}
	public String getElementName(){
		return unit.getElementName();
	}
	public List<String> getTypesName() throws JavaModelException {
		List<String> names = new ArrayList<String>();
		IType[] iTypes = unit.getAllTypes();
		for (IType x : iTypes) {
			for (Object y : getListTypes(x)) {
				names.add(getElementName(y));
			}
		}
		return names;
	}
	public String getElementName(Object obj) {
		if (obj instanceof IField) {
			IField y = (IField) obj;
			return y.getElementName();
		}
		if (obj instanceof IMethod) {
			IMethod y = (IMethod) obj;
			return y.getElementName();
		}
		if (obj instanceof IType) {
			IType y = (IType) obj;
			return y.getElementName();
		}
		return null;
	}
	List<Object> getListTypes(IType arg) throws JavaModelException {
		List<Object> result = new ArrayList<Object>();
		Object[] arrayMethods = arg.getFields();
		Object[] arrayFields = arg.getMethods();
		result.addAll(Arrays.asList(arrayMethods));
		result.addAll(Arrays.asList(arrayFields));
		result.add(arg);
		for (IType x : arg.getTypes()) {
			List<Object> deep = getListTypes(x);
			result.addAll(deep);
		}
		return result;
	}
	
}
